% ************************************************************************ 
%     Module name   : Lynx Simulation - main framework function                                                   
%                                                                          
%     Description   : This will house the simulation.                                                    
%                                                                          
%     $Header: /Lynx/SimulationFramework/RSP/IFS/IFSRun.m 2     15/10/08 16:08 Lwabeke $ 
%                                                                          
%    $Author: Lwabeke $	            
%                                                                         
%    $Revision: 2 $                
%                                                                         
% ************************************************************************
%     $History: IFSRun.m $ 
% 
%
% ************************************************************************

function [radarState] = radar_learn(changedInfo, old_radarState, reward, param)

radarState = old_radarState;

if (strcmp(changedInfo.radarSetup.mode,'Search') == 1)
    old_state_estimate = changedInfo.radarSetup.old_state_estimate;
    new_state_estimate = changedInfo.radarSetup.new_state_estimate;
    chosen_action = changedInfo.radarSetup.chosen_action;
    learning_rate = param.learn.search.learning_rate;
    inflation = param.learn.search.inflation;
    
    V = radarState.setup_select.Search.V(new_state_estimate);
    old_Q = radarState.setup_select.Search.Q(old_state_estimate, chosen_action);
    
    
    radarState.setup_select.Search.Q(old_state_estimate, chosen_action) = (1-learning_rate)*old_Q + learning_rate*(reward + inflation*V);
    
    %% Calculate expected rewards and policy
    [radarState.setup_select.Search.V, radarState.setup_select.Search.policy] = max(radarState.setup_select.Search.Q,[],2);      % Get V and policy as best values for each state
    
    %% Reduce exploration
   if (radarState.searchSetup.sector == 1)          % For now only only reduce when we get back to sector 1. If auto sector switching, this should be removed
     radarState.setup_select.Search.currentExplore = radarState.setup_select.Search.currentExplore * param.learn.search.explore_reduction;
   end
end